jQuery(document).ready(function($) {
   var sheetid = $("#sheetid").val();
   var qcount = $('#qcount').val();
   var pagecount = $('#pagecount').val();
   jQuery("#persontestform").validate({
   	ignore: ":hidden",
      submitHandler: function(form) {
         var target = jQuery(".persontest_page:visible");
         var cur_page = target.index() + 1;
         var next_page = cur_page + 1;
         var done_percentage = Math.round((cur_page/pagecount)*100) + '%';
         jQuery(".persontestprogress .bar").css("width", done_percentage);
         
         if(jQuery("#personTestSubmit").is(":hidden")) {
            jQuery(".qnum_done").text(cur_page*5);
            target.hide().next().fadeIn(600);
            if(next_page == pagecount) {
               jQuery("#nextSubmit").hide();
               jQuery("#personTestSubmit").show();
            }
            return false;
         } else {
            jQuery(".qnum_done").text(qcount);
         	$(form).ajaxSubmit({
               dataType: "json",
               beforeSubmit: function(data, form, options) {
                  if (!showLoading(form, options, null, {button:true}))
                     return false;
                  $("#errormsg").hide();
               },
               success: function(data) {
                  try {
                     var retmsg = data;
                     if (retmsg.error) {
                        $("#errormsg").jobUtil("showError", retmsg.error);
                     }
                     if (retmsg.success) {
                        $("#errormsg").jobUtil("showSuccess", '提交成功').fadeIn('fast');
                        $(window).unbind('beforeunload');
                        window.location = retmsg.redirect;
                     }
                  } catch(e) {
                     return false;
                  }
               }
            });
         }
      },
      errorElement: "span",
      errorPlacement: function (error, element) {
         error.appendTo(element.parent().parent().parent().find("span.error_container"));
      }
   });
   

   $(window).bind('beforeunload', function(){
	   if($("#winid_persontest").length <= 0)
		  return; 
	      $.ajax({
	    	 async: false,
	         type: 'POST',
	         url: OC.filePath('interview', 'ajax', 'emptytime.php'),
	         data: {sheetid: sheetid},
	         dataType: 'json',
	         success: function(result){
	            // do nothing
	         }
	      });
	   });   
   var total_time = parseInt(jQuery("#persontest_totaltime").val());
   var times = total_time * 60;
   var timeup = false; 
   var timeObj = window.setInterval(function(){     
      if (times <= 0) {        
         window.clearInterval(timeObj);
         $("#timeleft").html("您已超时！");
         return; 
      }
      // time text turn red!
      if (times <= 300 && timeup == false) {
         timeup = true;
         jQuery("#timeleft").addClass("person_timeup");
      }
      var text = padDate(Math.floor(times/60))+"分"+padDate(times%60)+"秒";
      $('#timeleft').html(text);
      times --;
   }, 1000); 
   
});